home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / tptc17sc.zip / UNINC.PAS < prev    next >
Pascal/Delphi Source File  |  1988-03-26  |  5KB  |  195 lines

  1.  
  2. (*
  3.  * uninc - post-processor for TPTC
  4.  *
  5.  * This program will read a TPTC output file and produce a new
  6.  * file without the inline include file contents.  The include
  7.  * files will be written along with the main file to the specified
  8.  * destination directory.
  9.  *
  10.  * S.H.Smith, 3/13/88  (rev. 3/13/88)
  11.  *
  12.  * Copyright 1988 by Samuel H. Smith;  All rights reserved.
  13.  *
  14.  *)
  15.  
  16. {$T+}    {Produce mapfile}
  17. {$R-}    {Range checking}
  18. {$B-}    {Boolean complete evaluation}
  19. {$S-}    {Stack checking}
  20. {$I+}    {I/O checking}
  21. {$N-}    {Numeric coprocessor}
  22. {$V-}    {Relax string rules}
  23. {$M 65500,16384,655360} {stack, minheap, maxhep}
  24.  
  25.  
  26. program TPTC_post_processor;
  27.  
  28. const
  29.    version1 =     'UNINC - Post-processor for TPTC';
  30.    version2 =     'Version 1.1 03/25/88    (C) 1988 S.H.Smith';
  31.  
  32. const
  33.    max_incl = 3;     {maximum include nesting}
  34.    bufsize = 20000;  {input file buffer size}
  35.    obufsize = 4000;  {output file buffer size}
  36.          
  37.                    {1234567890123456}
  38.    start_include = '/* TPTC: include';           
  39.    end_include   = '/* TPTC: end of ';
  40.    key_length    = 16;     {length(start_include)}
  41.  
  42. var
  43.    line:       string;     {current source line}
  44.    key:        string;     {current keyword}
  45.    name:       string;     {filenames}
  46.    
  47.    infd:       text;       {input file and buffer}
  48.    inbuf:      array[1..bufsize] of byte;
  49.  
  50.    destdir:    string;     {output directory and files}
  51.    ofd:        array[1..max_incl] of text;
  52.    obuf:       array[1..max_incl] of array[1..obufsize] of byte;
  53.    level:      integer;
  54.  
  55.  
  56.  
  57. (* ------------------------------------------------------------------ *)
  58. procedure init;
  59.    {parse command line, initialize global variables, open files}
  60. begin
  61.    if paramcount <> 2 then
  62.    begin
  63.       writeln('Usage:   uninc INFILE DESTDIR');
  64.       writeln('Example: unint test.c c:\tran');
  65.       halt;
  66.    end;
  67.  
  68.    {process input file}   
  69.    name := paramstr(1);
  70.    assign(infd,name);
  71.    {$i-} reset(infd); {$i+}
  72.    if ioresult <> 0 then
  73.    begin
  74.       writeln('Can''t open input file: ',name);
  75.       halt;
  76.    end;
  77.    setTextBuf(infd,inbuf);
  78.  
  79.    {process destination directory specification}      
  80.    destdir := paramstr(2);
  81.    if destdir[length(destdir)] <> '\' then
  82.       destdir := destdir + '\';
  83.  
  84.    {process initial output file}
  85.    name := destdir + name;
  86.    writeln(name);
  87.    level := 1;
  88.    assign(ofd[level],name);
  89.    {$i-} rewrite(ofd[level]); {$i+}
  90.    if ioresult <> 0 then
  91.    begin
  92.       writeln('Can''t create output file: ',name);
  93.       halt;
  94.    end;
  95.  
  96.    setTextBuf(ofd[level],obuf[level]);   
  97. end;
  98.  
  99.  
  100. (* ------------------------------------------------------------------ *)
  101. procedure enter_include;
  102. var
  103.    i: integer;
  104. begin       
  105.    {determine new include filename}
  106.    name := copy(line,18,99);        {/* tptc: include <filename> */}
  107.    name := copy(name,1,pos(' ',name)-1);
  108.    
  109.    {remove any directory specification fron the include filename}
  110.    if name[2] = ':' then
  111.       name := copy(name,3,99);
  112.    repeat
  113.       i := pos('\',name);
  114.       if i > 0 then name := copy(name,i+1,99);
  115.    until i = 0;
  116.    
  117.    {generate include statement in main file}
  118.    write(ofd[level],'#include "',name,'"');
  119.  
  120.    {display new include filename on screen}
  121.    name := destdir + name;
  122.    writeln(name);
  123.  
  124.    {create the new include file}
  125.    inc(level);
  126.    assign(ofd[level],name);
  127.    {$i-} rewrite(ofd[level]); {$i+}
  128.    if ioresult <> 0 then
  129.    begin
  130.       writeln('Can''t create include file: ',name);
  131.       halt;
  132.    end;
  133.  
  134.    setTextBuf(ofd[level],obuf[level]);   
  135. end;
  136.  
  137.  
  138. (* ------------------------------------------------------------------ *)
  139. procedure exit_include;
  140. begin
  141.    if level < 2 then
  142.       writeln('Improper include nesting (too many exits) (',line,')')
  143.    else
  144.    begin
  145.       close(ofd[level]);
  146.       dec(level);
  147.    end;
  148. end;
  149.  
  150.  
  151. (* ------------------------------------------------------------------ *)
  152. (*
  153.  * main procedure - initialize, process input, cleanup
  154.  *
  155.  *)
  156.         
  157. begin
  158.    {get things rolling}
  159.    writeln;
  160.    writeln(version1,'   ',version2);
  161.    init;
  162.  
  163.    {process each line in the file}   
  164.    while not eof(infd) do
  165.    begin
  166.       readln(infd,line);
  167.       
  168.       if pos('/* TPTC:',line) > 0 then
  169.          while line[1] = ' ' do
  170.             delete(line,1,1);
  171.             
  172.       key := copy(line,1,key_length);
  173.  
  174.       if key = start_include then
  175.          enter_include
  176.       else
  177.       if key = end_include then
  178.          exit_include
  179.       else
  180.          writeln(ofd[level],line);
  181.    end;
  182.    
  183.    {close files and terminate}
  184.    close(ofd[level]);
  185.    if level > 1 then
  186.    begin
  187.       writeln('unint: Premature eof');
  188.       repeat
  189.          dec(level);
  190.          close(ofd[level]);
  191.       until level = 1;
  192.    end;
  193. end.
  194.  
  195.